package com.fjec.devops.schedule.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.schedule.BaseScheduleHandler;
import com.fjec.common.util.DateUtil;
import com.fjec.devops.entity.StatAllUserNum;
import com.fjec.devops.entity.StatUnmarketUserNum;
import com.fjec.devops.feign.MarketClient;
import com.fjec.devops.service.IStatAllUserNumService;
import com.fjec.devops.service.IStatUnmarketUserNumService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class StatPersonHandler extends BaseScheduleHandler {

    @Resource
    private MarketClient marketClient;

    @Resource
    private IStatUnmarketUserNumService statUnmarketUserNumService;

    @Resource
    private IStatAllUserNumService statAllUserNumService;

    public Map<String, Object> handle(int days) {
        Map<String, Object> logMap = new HashMap<>();
        // 统计所有用户
        String allNumResult;
        try {
            int allNum = this.statAll(days);
            allNumResult = String.format("Handle all user number: [%d]", allNum);
        } catch (Exception ex) {
            allNumResult = String.format("Handle all user error: [%s], TraceId: [%s]."
                    , ex.getMessage(), this.error(ex));
        }
        // 统计普通用户
        String normalResult;
        try {
            int num = this.statNormal(days);
            normalResult = String.format("Handle normal user number: [%d]", num);
        } catch (Exception ex) {
            normalResult = String.format("Handle normal user error: [%s], TraceId: [%s]."
                    , ex.getMessage(), this.error(ex));
        }
        logMap.put("all", allNumResult);
        logMap.put("normal", normalResult);
        return logMap;
    }

    /**
     * 统计所有用户
     *
     * @param days
     * @return
     */
    private int statAll(int days) {
        List<Map<String, Object>> resultMapList = marketClient.statAllPersonalData(days);
        int num = 0;
        for (Map<String, Object> resultMap : resultMapList) {
            String strStatDate = MapUtils.getString(resultMap, "StatDate");
            Integer userNum = MapUtils.getInteger(resultMap, "Num");
            LocalDate statDate = DateUtil.parseLd(strStatDate, "yyyy-MM-dd");
            QueryWrapper<StatAllUserNum> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("StatDate", statDate);
            StatAllUserNum entity = statAllUserNumService.getOne(queryWrapper);
            if (entity == null) {
                entity = new StatAllUserNum();
                entity.setStatDate(statDate);
                entity.setUserNum(userNum);
                entity.setCreateTime(DateUtil.nowLdt());
            } else {
                entity.setUserNum(userNum);
                entity.setUpdateTime(DateUtil.nowLdt());
            }
            statAllUserNumService.saveOrUpdate(entity);
            num++;
        }
        return num;
    }

    /**
     * 统计普通用户
     *
     * @param days
     * @return
     */
    private int statNormal(int days) {
        List<Map<String, Object>> resultMapList = marketClient.statPersonalData(days);
        int num = 0;
        for (Map<String, Object> resultMap : resultMapList) {
            String strStatDate = MapUtils.getString(resultMap, "StatDate");
            Integer userNum = MapUtils.getInteger(resultMap, "Num");
            LocalDate statDate = DateUtil.parseLd(strStatDate, "yyyy-MM-dd");
            QueryWrapper<StatUnmarketUserNum> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("StatDate", statDate);
            StatUnmarketUserNum entity = statUnmarketUserNumService.getOne(queryWrapper);
            if (entity == null) {
                entity = new StatUnmarketUserNum();
                entity.setStatDate(statDate);
                entity.setUserNum(userNum);
                entity.setCreateTime(DateUtil.nowLdt());
            } else {
                entity.setUserNum(userNum);
                entity.setUpdateTime(DateUtil.nowLdt());
            }
            statUnmarketUserNumService.saveOrUpdate(entity);
            num++;
        }
        return num;
    }
}
